<template>
  <div>
    <el-row class="app-toptool" type="flex">
      <el-col :span="16">
        <Search
          size="mini"
          :search-visible="searchVisible"
          :search-data.sync="searchData"
          :search-form="searchForm"
          @refesh_list="searchgo"
        />
      </el-col>
      <el-col :span="8">
        <div class="btn-group" style="margin-bottom:11px;float: right;">
          <div>
            <el-button
              v-if="checkPermission('/supplier/add')"
              type="primary"
              size="mini"
              icon="el-icon-plus"
              @click="add()"
            >添加</el-button>
            <el-button
              v-if="checkPermission('/supplier/delete')"
              type="primary"
              :disabled="multiple"
              size="mini"
              icon="el-icon-delete"
              @click="del(ids)"
            >删除
            </el-button>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :row-class-name="rowClass"
        row-key="id"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        :border="false"
        :stripe="true"
        class="eltable"
        :data="list"
        style="width: 100%"
        @selection-change="selection"
      >
        <el-table-column align="center" type="selection" width="42" />
        <el-table-column align="center" type="" property="id" label="编号" show-overflow-tooltip width="70" />
        <el-table-column align="center" type="" property="title" label="名称" show-overflow-tooltip />
        <el-table-column align="center" type="" property="cate_name" label="分类名称" show-overflow-tooltip />
        <el-table-column align="center" type="" property="contacts" label="联系人" show-overflow-tooltip width="170" />
        <el-table-column align="center" type="" property="tel" label="联系电话" show-overflow-tooltip width="170" />
        <el-table-column align="center" type="" property="referee_person" label="推荐人" show-overflow-tooltip width="170" />
        <el-table-column align="center" type="" property="recommend" label="推荐人电话" show-overflow-tooltip width="170" />
        <el-table-column align="center" type="" property="divide" label="分成红利" show-overflow-tooltip width="170" >
          <template slot-scope="scope">
            {{scope.row.divide}} %

          </template>
        </el-table-column>
        <el-table-column align="center" type="" property="addr" label="地址" show-overflow-tooltip width="170" />
        <el-table-column align="center" type="" property="create_time" label="创建时间" show-overflow-tooltip width="170" />

        <el-table-column
          :fixed="$store.getters.device !== 'mobile'?'right':false"
          label="操作"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.id">
              <el-dropdown>
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">

                  <el-dropdown-item>
                    <el-button
                      v-if="checkPermission('/supplier/edit')"
                      size="mini"
                      type="primary"
                      @click="update(scope.row)"
                    ><i class="el-icon-edit" />修改</el-button>
                  </el-dropdown-item>

                </el-dropdown-menu>
              </el-dropdown>

            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="page_data.total"
        :page.sync="page_data.page"
        :limit.sync="page_data.limit"
        @pagination="index"
      />
    </div>

    <!--添加/修改-->
    <Update
      :info="updateInfo"
      :opentype="opentype"
      :show.sync="dialog.updateDialogStatus"
      size="mini"
      @refesh_list="index"
    />
  </div>
</template>
<script>
import Search from '@/components/common/Search'
import Pagination from '@/components/Pagination'
import Update from '@/views/supplier/update.vue'
import ImportData from '@/views/diyfields/importData.vue'
import {
  confirm,
  param2Obj
} from '@/utils/common'
import XLSX from 'xlsx'
export default {
  name: 'Storeindex',
  components: {
    Search,
    Pagination,
    Update,
    ImportData
  },
  data() {
    return {
      dialog: {
        updateDialogStatus: false,
        importDataDialogStatus: false
      },
      ids: [],
      single: true,
      multiple: true,

      list: [],
      opentype: 'add',
      updateInfo: {},
      loading: false,
      dumpshow: false,
      exceldata: [],
      dumppage: 1,
      percentage: 0,
      ws: {},
      filename: '',
      page_data: {
        limit: 20,
        page: 1,
        total: 20
      },
      searchVisible: true,
      searchForm: [],
      searchData: {},
      action: 'index',
      cateArray: []
    }
  },
  mounted() {
    this.index()
  },
  methods: {
    selection(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    searchgo() {
      this.page_data.page = 1
      this.index()
    },
    index() {
      const param = {
        path: this.$route.path,
        limit: this.page_data.limit,
        page: this.page_data.page
      }
      Object.assign(param, this.searchData)
      Object.assign(param, param2Obj(this.$route.fullPath))
      this.loading = true
      this.$api.post('/supplier/cate', {page:1, limit:10000}).then(res => {
        this.cateArray = res.data.item

      })
      this.$api.post('/supplier/index', param).then(res => {
        this.list = res.data.data
        this.page_data.total = res.data.count
        this.loading = false
        if (this.page_data.page == 1) {
          this.searchForm = [
            {
              type: 'Input',
              label: '名称|联系人|联系电话|地址 关键词',
              prop: 'keyword',
              width: '230px'
            },{
            type: 'Select',
            label: '分类',
            prop: 'cate',
            data: this.cateArray,
            width: '150px'
          }
          ]
        }
      })
    },
    add() {
      this.dialog.updateDialogStatus = true
      this.updateInfo = {}
    },
    update(row) {
      this.opentype = 'update'
      this.dialog.updateDialogStatus = true
      this.updateInfo = row
    },
    login(row) {
      const id = row.id ? row.id : this.ids.join(',')
      window.open('?sid=' + id + '#/dashboard', '_blank')
    },
    gosetusers(row) {
      const id = row.id ? row.id : this.ids.join(',')
      window.open('?sid=' + id + '#/users', '_blank')
    },
    del(row) {
      confirm({
        content: '确定要操作吗'
      }).then(() => {
        const ids = row.id ? row.id : this.ids.join(',')
        this.$api.post('/supplier/delete', {
          path: this.$route.path,
          id: ids
        }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.index()
        })
      }).catch(() => {})
    },
    importData() {
      this.dialog.importDataDialogStatus = true
    },
    dumpdata() {
      confirm({
        content: '确定要操作吗'
      }).then(() => {
        this.dumpshow = true
        this.confirmdumpdata()
      }).catch(() => {})
    },
    closedialog() {
      this.dumpshow = false
    },
    rowClass({
      row,
      rowIndex
    }) {
      for (let i = 0; i < this.ids.length; i++) {
        if (row.id === this.ids[i]) {
          return 'rowLight'
        }
      }
    }
  }
}
</script>
  <style scoped>
    .table_list_pic {
      width: 60px;
      height: 60px;
      vertical-align: middle;
    }
  </style>
